<template>
  <div class="question">
    <van-dropdown-menu>
      <van-dropdown-item v-model="city" ref="dropdown">
        <template #title>
          <div class="title"><span class="name">面试宝典</span>{{ city }}</div>
        </template>
        <template #default>
          <van-index-bar :index-list="indexList">
            <div v-for="(value, key, index) in citys" :key="index">
              <van-index-anchor :index="key">{{ key }}</van-index-anchor>
              <van-cell
                @click="cellClick(item2)"
                v-for="(item2, index2) in value"
                :key="index2"
                :title="item2"
              ></van-cell>
            </div>
          </van-index-bar>
        </template>
      </van-dropdown-item>
    </van-dropdown-menu>
    <div class="content">
      <div class="position">
        <van-tag
          v-for="(item, index) in cityPositions[city]"
          :key="index"
          class="tag"
          :class="{ active: currIndex === index }"
          @click="currIndex = index"
          color="#f7f4f5"
          text-color="#545671"
        >
          {{ item.name }}
        </van-tag>
      </div>
    </div>
    <div class="middle">
      <div class="top">
        <div class="item1">
          <div class="item-top">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item1-bottom">常错题库</div>
        </div>
        <div class="item1">
          <div class="item-top bgc2">
            <i class="iconfont">&#xe654;</i>
          </div>
          <div class="item1-bottom">收藏题库</div>
        </div>
      </div>

      <div class="top mt33">
        <div class="item1">
          <div class="item-top bgc3">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item1-bottom">常错题库</div>
        </div>
        <div class="item1">
          <div class="item-top bgc4">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item1-bottom">收藏题库</div>
        </div>
      </div>
      <div class="center">
        <!--
        van-circle
        v-model:变化过程中的当前值
        rate:目标值
        speed:速度(多少rate/秒 100rate/秒 1整圈要1秒)
        text:中间文本内容
        layer-color:轨道颜色
        color:进度条颜色
       -->
        <van-circle
          class="circle"
          v-model="currRate"
          :rate="getRate(total)"
          :speed="5"
          layer-color="none"
          :color="gradientColor"
        >
          <div class="txt">
            <div>顺序刷题</div>
            <div>{{ getQuestions }}/{{ total }}</div>
          </div>
        </van-circle>
      </div>
    </div>
    <div class="total">
      <h3>累计收录</h3>
      <!-- toLocaleString 当数字很多的时候，调用方法会自动用逗号隔开 -->
      <div>{{ total.toLocaleString() }}</div>
    </div>
    <div class="btn">
      <van-button block color="#00b8d4" @click="btnClick">模拟面试</van-button>
    </div>
  </div>
</template>

<script>
import { interviewFilters } from '@/api/question'
import { mapGetters } from 'vuex'
export default {
  data () {
    return {
      step: 0, // 0获取题目 但还没有答题 1表示已答题，但还没有点提交 2 已提交，但不是最后一题 3已提交且是最后一题
      city: '全国',
      citys: '', // 存储数据
      indexList: [],
      cityPositions: '',
      currIndex: 0, // 选中的岗位索引
      currRate: 0,
      total: 0, // 题目总数
      // 轨道渐变色
      gradientColor: {
        '0%': 'rgba(228,1,55,0.7)',
        '100%': 'rgba(228,1,55,0.3)'
      }
    }
  },
  async created () {
    // 获取数据
    const res = await interviewFilters()
    console.log(res)
    // 存储数据
    this.citys = res.data.data.citys
    // 存储岗位数据
    this.cityPositions = res.data.data.cityPositions
    // 存储total题目总数
    this.total = res.data.data.totalCount
    // 将对象的key转换成数据给索引列表
    this.indexList = Object.keys(res.data.data.citys)
  },
  mounted () {
    document.body.style.borderRightColor = '#f7f4f5'
  },

  methods: {
    cellClick (item) {
      // 让city=当前点击城市名
      this.city = item
      // 关闭弹窗，需要在dropdown-item上定义一个ref=dropdown
      this.$refs.dropdown.toggle()
      // 切换城市时，还原选中项
      this.currIndex = 0
    },
    // 点击模拟面试跳转到刷题页
    btnClick () {
      // 路由传值 id 和 city  id是使用动态路由匹配，需要在路由中配置：id
      if (this.indexList.length) {
        const _id = this.cityPositions[this.city][this.currIndex].id
        this.$router.push('/home/questionlnfo/' + _id + '?city=' + this.city)
      }
      // 职位id用动态路由配置传
      // 1:配置路由 :id   2: 传 this.$router.push('/path/:id值') 3: 收： this.$route.params.id
      // city用普通 路由传参 this.$router.push('/path/参数名=值') this$route.query.参数名
    }
  },
  computed: {
    ...mapGetters(['getQuestions', 'getRate'])
  }
}
</script>

<style lang="less" scoped>
.question {
  .title {
    .name {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: center;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
  }
  .content {
    padding: 25px @p15;
    .position {
      overflow: auto;
      display: flex;
      align-items: center;
      .tag {
        flex: 0 0 auto;
        padding: 5px 8px;
        margin-right: 15px;
        border-radius: 32px;
      }
      .active {
        background-color: #00b8d4 !important;
        color: #feffff !important;
      }
    }
  }
  .middle {
    position: relative;
    margin-top: 10px;
    background-color: #fff;
    padding: 16px 43px;
    .top {
      display: flex;
      justify-content: space-between;
      .item1 {
        width: 50px;
        text-align: center;
        .item-top {
          display: inline-block;
          width: 44px;
          height: 44px;
          border-radius: 50%;
          background-color: #ff5f5f;
          text-align: center;
          line-height: 44px;
          .iconfont {
            font-size: 24px;
            color: #ffff;
          }
        }
        .bgc2 {
          background-color: #ffcb03;
        }
        .bgc3 {
          background-color: #00c7de;
        }
        .bgc4 {
          background-color: #2cd593;
        }
        .item1-bottom {
          display: inline-block;
          padding-top: 7px;
          font-size: 12px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          color: #545671;
          line-height: 16px;
          letter-spacing: 0px;
        }
      }
    }
    .mt33 {
      margin-top: 33px;
    }
  }
  .center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-image: url(~@/style/tu.png);
    width: 120px;
    height: 120px;
    background-size: 100%;
    .circle {
      width: 100% !important;
      height: 100% !important;
      padding: 10px;
    }
    .txt {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      background: linear-gradient(180deg, #ff6f92, #e40137);
      color: #fff;
      font-size: 20px;
      padding-top: 25px;
    }
  }
  .total {
    margin-top: 15px;
    margin-bottom: 15px;
    background-color: #fff;
    text-align: center;
    font-size: 20px;
    h3 {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      color: #545671;
      line-height: 25px;
      letter-spacing: 0px;
    }
    div {
      padding-bottom: 20px;
    }
  }
  .btn {
    padding: 0 @p15;
    border-radius: 7px;
  }
}
</style>
